package com.example.li.utils;

import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.example.li.FllowApplication;
import com.example.li.bean.LocationInfo;
import com.example.li.bean.MessageEvent;

import de.greenrobot.event.EventBus;

/**
 */
public class LocationUtil {
    private static LocationUtil mLocationUtil = null;
    public LocationClient mLocationClient = null;
    public BDLocationListener myListener = new MyLocationListener();
    private LocationInfo info;
    public static final String ACTION_LOCATION_CHANGE = "action_location_change";

    public LocationInfo getInfo() {
        return info;
    }

    public void setInfo(LocationInfo info) {
        this.info = info;
    }

    private LocationUtil() {
        mLocationClient = new LocationClient(FllowApplication.context);     //声明LocationClient类
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
        );//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系
        int span = 1000 * 60 * 2; // 2分钟
        option.setScanSpan(0);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当GPS有效时按照1S/1次频率输出GPS结果
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        option.setIgnoreKillProcess(false);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        option.SetIgnoreCacheException(false);//可选，默认false，设置是否收集CRASH信息，默认收集
        option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤GPS仿真结果，默认需要
        mLocationClient.setLocOption(option);
        mLocationClient.registerLocationListener(myListener);    //注册监听函数
    }

    public static LocationUtil getInstance() {
        if (null == mLocationUtil) {
            mLocationUtil = new LocationUtil();
        }
        return mLocationUtil;
    }

    public void startLocation() {
        mLocationClient.start();
    }

    public LocationInfo getlocation() {
        return getInfo();
    }


    public class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            String addr = location.getAddrStr();    //获取详细地址信息
            String country = location.getCountry();    //获取国家
            String province = location.getProvince();    //获取省份
            String city = location.getCity();    //获取城市
            String district = location.getDistrict();    //获取区县
            String street = location.getStreet();    //获取街道信息
            Log.d("main", addr + country + province);
            info = new LocationInfo.Builder()
                    .addr(addr)
                    .country(country)
                    .province(province)
                    .city(city)
                    .district(district)
                    .street(street)
                    .latitude(location.getLatitude() + "")
                    .lontitude(location.getLongitude() + "")
                    .radius(location.getRadius()+"")
                    .build();
            EventBus.getDefault().post(new MessageEvent(info));

        }
    }


}
